package org.judo.generate.mvc;

public class DisplayParser implements Parser
{
	ViewResult viewResult;
	private String curStatement;
	
	String captionSize;
	String labelSize;
	
	public ViewResult getViewResult()
	{
		return viewResult;
	}

	public void setViewResult(ViewResult viewResult)
	{
		this.viewResult = viewResult;
	}

	public Parser getNestedParser()
	{
		return null;
	}

	public boolean isFinished()
	{
		if (curStatement != null)
			if (curStatement.equals("/display"))
				return true;
				
		return false;
	}

	public boolean needsNestedParser()
	{
		return false;
	}

	public void parseNextLine(String line) throws ParseException
	{
		curStatement = line;
		
		if (line.startsWith("/display"))
		{
			viewResult.html += "\t\t\t\t</table>\n";
		}
		else if (line.equals(""))
		{
		}
		else
		{
			String value = ParserUtil.parseFreeText(line,viewResult);
			value = value.trim();
			
			String parts[] = value.split(",");
			
			if (parts.length != 2)
				throw new ParseException("Displays must have two values delimited by a comma.");
			
			String label = parts[1];
			String caption = parts[0];
			
			startField();
			add("<b>"+caption+"</b>",captionSize);
			space();
			add(label,labelSize);
			endField();
		}
	}
	
	protected void startField()
	{
		viewResult.html += "\t\t\t\t\t<tr>\n";
	}
	
	protected void endField()
	{
		viewResult.html += "\t\t\t\t\t</tr>\n";
		startField();
		space();
		space();
		space();
		viewResult.html += "\t\t\t\t\t</tr>\n";
	}
	
	protected void add(String controlHTML,String size)
	{
		viewResult.html += "\t\t\t\t\t\t<td width=\""+size+"\">\n";
		viewResult.html += "\t\t\t\t\t\t\t"+controlHTML+"\n";
		viewResult.html += "\t\t\t\t\t\t</td>\n";
	}
	
	protected void space()
	{
		viewResult.html += "\t\t\t\t\t\t<td>&nbsp;&nbsp;</td>\n";
	}
	
	public void setInitLine(String line) throws ParseException
	{
		String value = ParserUtil.getValue(line,viewResult);
		value = value.trim();
		
		String parts[] = value.split(",");
		
		if (parts.length != 2)
			throw new ParseException("Displays must have two parameters.  A caption size and a label size.");
		
		captionSize = parts[0];
		labelSize = parts[1];
		
		viewResult.html += "\t\t\t\t<table>\n";
	}
}
